package com.example.traning.mall.framework.common.pojo;

import com.example.traning.mall.framework.common.ex.ErrorCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * 统一返回
 *
 * @author lei yu
 * @since 2025/04/27
 */
@Schema(description = "公共返回")
@Data
public class R {
    @Schema(description = "错误码", example = "0")
    private Integer code;
    @Schema(description = "错误提示，用户可阅读")
    private String msg;
    @Schema(description = "返回数据")
    private Object data;

    public static R ok() {
        return new R().setCode(0).setMsg("成功");
    }

    public static R ok(Object data) {
        return ok().setData(data);
    }

    public static R error(String msg) {
        return new R().setCode(500).setMsg(msg);
    }

    public static R error(Integer code, String msg) {
        return new R().setCode(code).setMsg(msg);
    }

    public static R error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }
}
